package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.item.TbItemExample;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.goods.GoodsDescService;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.service.item.ItemService;
import com.dongdongshop.service.itemCat.ItemCatService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Eayon
 * @Date 2020/3/5 16:29
 */
@Component
@RocketMQMessageListener(topic = "freemarker-topic",consumerGroup = "my-consumer")
public class ItemPageServiceImpl implements RocketMQListener<String> {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDescService goodsDescService;
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private ItemService itemService;

    @Value("${freemarker.outputPath}")
    private String outputPath;

    Logger log = LoggerFactory.getLogger(ItemPageServiceImpl.class);


    /**
     * 消费者方法 生成商品的静态页面
     * @param id    接收到的商品id消息
     */
    @Override
    public void onMessage(String id) {
        log.info("生产freemarker静态页方法接收到商品id消息：{}",id);
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            //创建模板对象
            Template template = configuration.getTemplate("item.ftl");
            //根据商品id查询商品及商品详情信息
            TbGoods goods = goodsService.getGoodsById(Long.valueOf(id));
            TbGoodsDesc goodsDesc = goodsDescService.getGoodsDescById(Long.valueOf(id));
            //根据商品id查询该商品的三级分类
            TbItemCat itemCat1 = itemCatService.getItemCatById(goods.getCategory1Id());
            TbItemCat itemCat2 = itemCatService.getItemCatById(goods.getCategory2Id());
            TbItemCat itemCat3 = itemCatService.getItemCatById(goods.getCategory3Id());
            //查询该商品的sku信息(item表)
            TbItemExample example = new TbItemExample();
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andStatusEqualTo("1");
            criteria.andGoodsIdEqualTo(Long.valueOf(id));
            List<TbItem> items = itemService.selectItemList(example);
            //创建数据模型
            Map map = new HashMap();
            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);
            map.put("items",items);
            //创建输出流 也就等于E:\\freemarkerPage\\ + id.html
            FileWriter fileWriter = new FileWriter(outputPath + id + ".html");
            template.process(map,fileWriter);
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成商品的静态页面
     * @param id 商品id
     */
    /*public void generateItemPage(Long id) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            //创建模板对象
            Template template = configuration.getTemplate("item.ftl");
            //根据商品id查询商品及商品详情信息
            TbGoods goods = goodsService.getGoodsById(id);
            TbGoodsDesc goodsDesc = goodsDescService.getGoodsDescById(id);
            //根据商品id查询该商品的三级分类
            TbItemCat itemCat1 = itemCatService.getItemCatById(goods.getCategory1Id());
            TbItemCat itemCat2 = itemCatService.getItemCatById(goods.getCategory2Id());
            TbItemCat itemCat3 = itemCatService.getItemCatById(goods.getCategory3Id());
            //查询该商品的sku信息(item表)
            TbItemExample example = new TbItemExample();
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andStatusEqualTo("1");
            criteria.andGoodsIdEqualTo(id);
            List<TbItem> items = itemService.selectItemList(example);
            //创建数据模型
            Map map = new HashMap();
            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);
            map.put("items",items);
            //创建输出流 也就等于E:\\freemarkerPage\\ + id.html
            FileWriter fileWriter = new FileWriter(outputPath + id + ".html");
            template.process(map,fileWriter);
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
